package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.mappers.UserMapper;
import com.tanhua.model.domain.User;
import com.tanhua.service.UserApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author げっしょく
 * @date 2021/10/21
 */

@DubboService
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    /**
     * 通过手机号码查询用户
     * @param phone
     * @return
     */
    @Override
    public User getByPhone(String phone) {
        LambdaQueryWrapper<User> lam = new LambdaQueryWrapper<>();
        lam.eq(User::getMobile, phone);
//        User user = userMapper.selectByPhone(phone);
//        return users.get(0);
        User user = userMapper.selectOne(lam);
        return user;
    }

    /**
     * 保存用户并返回id
     * @param user
     * @return
     */
    @Override
    public Long save(User user) {
        userMapper.insert(user);
        return user.getId();
    }

    /**
     * 修改用户手机号码
     * @param user
     */
    @Override
    public void updateById(User user) {
        userMapper.updateById(user);
    }

    //通过用户id查询用户
    @Override
    public User getByID(Long id) {
        User user = userMapper.selectById(id);
        return user;
    }

    //通过用户id修改用户信息
    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    //通过环信账号查找用户
    @Override
    public User getUser(String huanxinId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("hx_user", huanxinId);
        User user = userMapper.selectOne(qw);
        return user;
    }

    //查询累计用户
    @Override
    public Integer getCountUser() {
        Integer integer = userMapper.selectCount(null);
        return integer;
    }
}
